package com.example.demo.sole;

import com.alibaba.fastjson.JSONObject;
import com.amazonaws.services.dynamodbv2.xspec.S;
import com.fasterxml.jackson.databind.JsonNode;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 类目抓取
 */
public class category {

    public static void main(String[] args) throws Exception {

        int i = 0;
        List<SoleCategory> soleCategorys = new ArrayList<>();

        Document doc = Jsoup.connect("https://e.51sole.com/").get();
        Elements container = doc.getElementsByClass("sidebar_con");
        Elements category1s = container.select(".sidebar_item > dd");
        for (Element category1 : category1s) {
            if (i > 34) {
                break;
            }
            String category1name = category1.getElementsByTag("a").text();
            String category1url = category1.getElementsByTag("a").attr("href");

            Document categoryDoc = Jsoup.connect(category1url).get();
            Elements category2s = categoryDoc.select(".hyz_fl_info > .hyz_fl");

            SoleCategory soleCategory1 = new SoleCategory();
            List<SoleCategory> child2 = new ArrayList<>();
            soleCategory1.name = category1name;

            for (Element category2 : category2s) {
                String category2name = category2.selectFirst(".hyz_fl_t").text();
                Elements category3s = category2.select(".hyz_fl_b > a");

                SoleCategory soleCategory2 = new SoleCategory();
                List<SoleCategory> child3 = new ArrayList<>();
                soleCategory2.name = category2name;

                for (Element category3 : category3s) {
                    String category3name = category3.getElementsByTag("a").text();

                    SoleCategory soleCategory3 = new SoleCategory();
                    soleCategory3.name = category3name;
                    child3.add(soleCategory3);
                }
                child2.add(soleCategory2);
                soleCategory2.childNames = child3;
            }
            soleCategory1.childNames = child2;
            soleCategorys.add(soleCategory1);
            i++;
        }

        System.out.println(JSONObject.toJSONString(soleCategorys));
    }

    private static class SoleCategory {

        private String name;
        private List<SoleCategory> childNames;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<SoleCategory> getChildNames() {
            return childNames;
        }

        public void setChildNames(List<SoleCategory> childNames) {
            this.childNames = childNames;
        }
    }
}
